package PCClient2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @Author: 0162977
 * @Date: 2020/12/2 16:03
 */
public class SendThread implements Runnable{

    @Override
    public void run() {
        //定义服务器的地址，端口号，数据
        InetAddress address = null;
        try {
            address = InetAddress.getByName("localhost");
            String secondS = null;

            byte[] data = null;//将字符串转换为字节数组
            //创建数据报
            for (int i = 0 ;i<10000 ;i++){
                if (i%2 == 0){
                    secondS = "edu";
                }else{
                    secondS = "org";
                }
                int random = (int) (Math.random()*100);
                System.out.println(random +"."+secondS+".cn");
                ShareSpaceAndFun.addReqMap(random +"."+secondS+".cn");
                data =( "1,"+String.valueOf(random)+"."+secondS+".cn,"+ ShareSpaceAndFun.myPort).getBytes();
                DatagramPacket packet = new DatagramPacket(data, data.length, address, ShareSpaceAndFun.portMainLocal);
                //创建DatagramSocket，实现数据发送和接收
                DatagramSocket socket = new DatagramSocket();
                //向服务器端发送数据报
                socket.send(packet);
                socket.close();
                Thread.sleep(5);
                System.out.println(i);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
